package Player
{
	import BasicEngine.Dispatcher;
	import BasicEngine.KeyMouseManager;
	import BasicEngine.MsgManager;
	import DataStore.DataSource;
	import fl.events.ColorPickerEvent;
	import flash.events.Event;
	import flash.geom.Point;
	import GameScheduler.Game;
	import mx.resources.ResourceManagerImpl;
	import BasicEngine.MsgId;
	//具有动画的实体处理
	public class AnimationProc extends BaseProc
	{
		public static var Instance:AnimationProc = new AnimationProc();
		public function AnimationProc()
		{
		}
		
		public override function Update(_msg:MsgId):void
		{
			if (_msg.AcceptID > 0)  // 不等于 -1,不等于0   //个体消息
			{
				var data:AnimationEntData = DataCenter.GetData(_msg.AcceptID) as AnimationEntData;
				if (data != null)
				{					
				   trace("ok"); //执行消息
				}
			}			
		}
		
		public function SwitchAnimation(_msg:MsgId):void
		{
			if (_msg.AcceptID > 0)  // 不等于 -1,不等于0   //个体消息
			{
				var data:AnimationEntData = DataCenter.GetData(_msg.AcceptID) as AnimationEntData;
				if (data != null)
				{		
				   var animationName:String = _msg.Data.toString();
				   data.SwitchAnimation(animationName);
				}
			}				
		}
		
		public function MoveToDistination(_msg:MsgId):void
		{
			if (_msg.AcceptID > 0)  // 不等于 -1,不等于0   //个体消息
			{
				var data:AnimationEntData = DataCenter.GetData(_msg.AcceptID) as AnimationEntData;
				if (data != null)
				{		
				   
				}
			}
		}
		
		/**
		 * 从当前点直线走到目标点
		 */
		public function P2P(_destination:Point,_data:AnimationEntData):void
		{
			P2P_X(_destination, _data);
		}
		
		private function P2P_X(_destination:Point,_data:AnimationEntData):void
		{
			if (_destination.x < _data.X)
			  {
			       _data.VelocityX = -100;
				   _data.SwitchAnimation("MoveWest");
			  }
			  else if (_destination.x > _data.X)
			  {
			      _data.VelocityX = 100;
				  _data.SwitchAnimation("MoveEast")	;
			  }
			if (Math.abs(_destination.x - _data.X)>3)
			{			  
			  
			}		
		}
		
		private static function P2P_Y(_destination:Point,_data:AnimationEntData):void
		{
			if (_destination.y < _data.Y)
			  {
			       _data.VelocityY = -100;
				   _data.SwitchAnimation("MoveNorth");
			  }
			  else if (_destination.y > _data.Y)
			  {
			      _data.VelocityY = 100;
				  _data.SwitchAnimation("MoveSouth")	;
			  }
			if (Math.abs(_destination.y - _data.Y)>3)
			{			  
			  
			}		
		}
		
		private function ProblemRoute():void
		{
			
		}
		
		public function KeyDown(_msg:MsgId):void
		{
			trace("key down");
			var data:AnimationEntData = DataCenter.GetData(_msg.AcceptID) as AnimationEntData;
			var keyCode:int = int(_msg.Data);
			if (keyCode == data.keycode)
			{
				return ;
			}
			else
			{
				data.keycode = keyCode;
			}
			if (keyCode == 39)
			{
			   data.SwitchAnimation("MoveEast");
			   
			   data.VelocityX = 100;
			   data.VelocityY = 0;
			}
			else if (keyCode == 37)
			{
			   data.SwitchAnimation("MoveWest");
			   data.VelocityX = -100;
			   data.VelocityY = 0;
			}
		}
		
		public function KeyUp(_msg:MsgId):void
		{
			var data:AnimationEntData = DataCenter.GetData(_msg.AcceptID) as AnimationEntData;
			data.keycode = -1;
			var keyCode:int = int(_msg.Data);
			

			
			if (keyCode == 39)
			{
			   data.SwitchAnimation("StandEast");
			   data.VelocityX =  0;
			   data.VelocityY =  0;
			   
			}
			else if (keyCode == 37)
			{
			   data.SwitchAnimation("StandWest");
			   data.VelocityX =  0;
			   data.VelocityY =  0;			   
			}
		}
		
		public function UpdateFrame(_data:AnimationEntData):void
		{
			var anim:AnimationUnit = _data.CurrentAnimation;			
			anim.delayConter += Dispatcher.Clock.Tick;
			if (anim.delayConter >= anim.Delay)
			{
				anim.delayConter = 0;
			    var fr:Vector.<FrameUnit> = anim.Frames;
			    anim.i++;
			    if (anim.i >= anim.Frames.length)
			        anim.i = 0;
			    	
			    _data.Img.bitmapData = _data.BmpdataFrames[anim.Frames[anim.i].ImgSourceID];	
				
				if (anim.Frames[anim.i].ImgSourceEffect == 0)
				{ 
					_data.Img.scaleX = 1;
					_data.Img.scaleY = 1;
					_data.Img.x = anim.Frames[anim.i].OffsetX;
			        _data.Img.y = anim.Frames[anim.i].OffsetY;			
				}
				else if (anim.Frames[anim.i].ImgSourceEffect == 1)
				{
				     _data.Img.scaleX = -1;
				     _data.Img.x = anim.Frames[anim.i].OffsetX + _data.Img.bitmapData.width;
					 _data.Img.y = anim.Frames[anim.i].OffsetY;		
				}
				else if (anim.Frames[anim.i].ImgSourceEffect == 2)
				{
				     _data.Img.scaleY = -1;
				     _data.Img.y = anim.Frames[anim.i].OffsetY + _data.Img.bitmapData.height;
					 _data.Img.x = anim.Frames[anim.i].OffsetX;
				}
				else if (anim.Frames[anim.i].ImgSourceEffect == 3)
				{
				     _data.Img.scaleY = -1;
					 _data.Img.scaleX = -1;
				     _data.Img.y = anim.Frames[anim.i].OffsetY + _data.Img.bitmapData.height;
					 _data.Img.x = anim.Frames[anim.i].OffsetX + _data.Img.bitmapData.width;
				}
			}			
			
			if(_data.move)
			   UpdataP2P(_data);
			
			UpdatePos(_data);
			
			_data.ImgContainer.x = _data.X + _data.footOffset.x;
			_data.ImgContainer.y = _data.Y + _data.footOffset.y;
			_data.ImgContainer.visible = !_data.Hide;
		}
		
		/**
		 * 当速度不为零时，更新实体坐标
		 */
		private function UpdatePos(_data:AnimationEntData):void
		{
			if(_data.VelocityX  != 0) 
			{
				var dx:int = _data.VelocityX *   (Dispatcher.Clock.Tick / 1000);
				_data.X += dx;
			}	
			if(_data.VelocityY  != 0) 
			{
				var dy:int = _data.VelocityY *  (Dispatcher.Clock.Tick / 1000);
				_data.Y += dy;
			}
		}
		
		private function UpdataP2P(_data:AnimationEntData):void
		{
			if (Math.abs(_data.target.x - _data.X) > 3)//x坐标未达到
			{
				P2P_X(_data.target, _data);
			}
			else if (Math.abs(_data.target.y - _data.Y) > 3)//x坐标已达到，但y坐标未达到
			{
				_data.VelocityX = 0;
				P2P_Y(_data.target, _data);
			}	
			else
			{
				_data.VelocityX = 0;
				_data.VelocityY = 0;
				_data.move = false;
				
				_data.SwitchAnimation("StandSouth");
			}
		}
		
	}
}